{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ExprRules.jl\n",
    "\n",
    "This is the base package to support the generation and optimization of Julia expressions from a grammar.  The package contains many basic functions for declaring and working with grammars and expression trees.\n",
    "\n",
    "## Installation\n",
    "\n",
    "    Pkg.add(\"ExprRules\")\n",
    "\n",
    "Once it's installed, start using the package by calling:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "using ExprRules"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Usage\n",
    "\n",
    "### Define a grammar\n",
    "\n",
    "Grammars are specified by production rules that specify substitutions of non-terminal symbols.  Each production rule is an equality with a non-terminal on the left side and a Julia expression on the right side.  \n",
    "\n",
    "The _() syntax is a special function where the argument is evaluated at the time of derivation tree's construction and the value is held constant throughout the life of the tree.  The pipe (|) syntax is a short-hand that allows the user to define multiple production rules on a single line (i.e., Backus-Naur Form).  The |() syntax is another similar short-hand that takes a collection as argument and creates a production rule for each element in the collection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1: Real = x\n",
       "2: Real = Real * Real\n",
       "3: Real = f(Real)\n",
       "4: Real = _(Base.rand(1.0:5.0))\n",
       "5: Real = A\n",
       "6: Real = B\n",
       "7: Real = g(Real)\n",
       "8: Real = 1\n",
       "9: Real = 2\n",
       "10: Real = 3\n",
       "11: Real = 4\n",
       "12: Real = 5\n",
       "13: Real = 6\n",
       "14: Real = 7\n",
       "15: Real = 8\n",
       "16: Real = 9\n"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grammar = @grammar begin\n",
    "    Real = x                      # symbol\n",
    "    Real = Real * Real            # julia expression\n",
    "    Real = f(Real)                # function call\n",
    "    Real = _(Base.rand(1.0:5.0))  # special syntax, eval argument of _() at derivation time\n",
    "    Real = A | B | g(Real)        # multiple rules on a single line\n",
    "    Real = 1 | 2 | 3\n",
    "    Real = |(4:6)                 # same as Real = 4 | 5 | 6\n",
    "    Real = |([7,8,9])             # same as Real = 7 | 8 | 9 \n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Grammar helper functions\n",
    "\n",
    "List non-terminals of the grammar:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1-element Array{Symbol,1}:\n",
       " :Real"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nonterminals(grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Grammars are indexed by non-terminal symbols and return the corresponding production rule numbers with that nonterminal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16-element Array{Int64,1}:\n",
       "  1\n",
       "  2\n",
       "  3\n",
       "  4\n",
       "  5\n",
       "  6\n",
       "  7\n",
       "  8\n",
       "  9\n",
       " 10\n",
       " 11\n",
       " 12\n",
       " 13\n",
       " 14\n",
       " 15\n",
       " 16"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grammar[:Real]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Get the return type of the first production rule:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":Real"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "return_type(grammar, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get the number of children of the second production rule:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nchildren(grammar, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get the child types of the second production rule:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Array{Symbol,1}:\n",
       " :Real\n",
       " :Real"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "child_types(grammar, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get the maximum number of children (arity) of the grammar:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max_arity(grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Determine if the third production rule is terminal:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "false"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isterminal(grammar, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Determine if the fourth production rule is a special _() function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iseval(grammar, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Expression trees\n",
    "\n",
    "An expression tree represents the derivation of an expression as a tree.  The nodes in an expression tree contain an index to a production rule.\n",
    "\n",
    "Define an expression tree manually:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"11.871pt\" height=\"41.105pt\" viewBox=\"0 0 11.871 41.105\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390634-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390634-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.359375 -5.546875 C 4.359375 -6.15625 3.5625 -6.15625 3.40625 -6.15625 C 2.609375 -6.15625 1.828125 -5.6875 1.828125 -4.828125 L 1.828125 -4.296875 L 0.84375 -4.296875 C 0.671875 -4.296875 0.421875 -4.296875 0.421875 -3.984375 C 0.421875 -3.6875 0.671875 -3.6875 0.828125 -3.6875 L 1.828125 -3.6875 L 1.828125 -0.609375 L 0.828125 -0.609375 C 0.671875 -0.609375 0.421875 -0.609375 0.421875 -0.3125 C 0.421875 0 0.671875 0 0.828125 0 L 3.53125 0 C 3.671875 0 3.9375 0 3.9375 -0.296875 C 3.9375 -0.609375 3.671875 -0.609375 3.53125 -0.609375 L 2.515625 -0.609375 L 2.515625 -3.6875 L 3.734375 -3.6875 C 3.890625 -3.6875 4.140625 -3.6875 4.140625 -3.984375 C 4.140625 -4.296875 3.890625 -4.296875 3.734375 -4.296875 L 2.515625 -4.296875 L 2.515625 -4.765625 C 2.515625 -5.546875 3.1875 -5.546875 3.484375 -5.546875 C 3.484375 -5.5 3.578125 -5.109375 3.921875 -5.109375 C 4.125 -5.109375 4.359375 -5.28125 4.359375 -5.546875 Z M 4.359375 -5.546875 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390634-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.8125 -1.671875 C 4.8125 -2.359375 4.328125 -3 3.515625 -3.1875 C 4.15625 -3.40625 4.59375 -3.921875 4.59375 -4.546875 C 4.59375 -5.28125 3.984375 -6.09375 2.875 -6.09375 L 0.625 -6.09375 C 0.484375 -6.09375 0.234375 -6.09375 0.234375 -5.796875 C 0.234375 -5.484375 0.484375 -5.484375 0.640625 -5.484375 L 0.90625 -5.484375 L 0.90625 -0.609375 L 0.640625 -0.609375 C 0.484375 -0.609375 0.234375 -0.609375 0.234375 -0.296875 C 0.234375 0 0.484375 0 0.625 0 L 3.078125 0 C 4.203125 0 4.8125 -0.890625 4.8125 -1.671875 Z M 3.90625 -4.53125 C 3.90625 -4.125 3.59375 -3.46875 2.625 -3.46875 L 1.59375 -3.46875 L 1.59375 -5.484375 L 2.84375 -5.484375 C 3.6875 -5.484375 3.90625 -4.859375 3.90625 -4.53125 Z M 4.109375 -1.6875 C 4.109375 -1.203125 3.734375 -0.609375 2.859375 -0.609375 L 1.59375 -0.609375 L 1.59375 -2.859375 L 2.96875 -2.859375 C 3.8125 -2.859375 4.109375 -2.140625 4.109375 -1.6875 Z M 4.109375 -1.6875 \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 0.0015 -6.594281 L 0.0015 -21.32475 \" transform=\"matrix(1,0,0,-1,5.936,6.394)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.195653 1.59525 C -1.09409 0.997594 -0.00034 0.0991562 0.300441 0.0015 C -0.00034 -0.100063 -1.09409 -0.994594 -1.195653 -1.59225 \" transform=\"matrix(0,1,1,0,5.936,27.71909)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390634-0-1\" x=\"3.321\" y=\"9.468\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390634-0-2\" x=\"3.321\" y=\"37.784\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "TreeView.LabelledTree({2, 1} directed simple Int64 graph, Any[:f, :B])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rulenode = RuleNode(3, [RuleNode(6)])\n",
    "display(rulenode, grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate a random expression tree from the grammar:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"45.448pt\" height=\"40.741pt\" viewBox=\"0 0 45.448 40.741\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390635-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390635-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.546875 -2.140625 C 4.546875 -2.296875 4.421875 -2.390625 4.40625 -2.40625 C 4.15625 -2.578125 3.484375 -2.90625 3.21875 -3.046875 L 4.296875 -3.625 C 4.421875 -3.703125 4.546875 -3.765625 4.546875 -3.953125 C 4.546875 -4 4.546875 -4.28125 4.140625 -4.28125 L 2.90625 -3.5625 C 2.9375 -3.828125 2.9375 -4.484375 2.9375 -4.78125 C 2.9375 -4.859375 2.9375 -5.1875 2.609375 -5.1875 C 2.28125 -5.1875 2.28125 -4.859375 2.28125 -4.78125 C 2.28125 -4.484375 2.296875 -3.828125 2.3125 -3.5625 L 1.234375 -4.203125 C 1.09375 -4.28125 1.078125 -4.28125 1 -4.28125 C 0.8125 -4.28125 0.671875 -4.109375 0.671875 -3.953125 C 0.671875 -3.765625 0.78125 -3.703125 0.921875 -3.640625 L 2 -3.046875 L 0.921875 -2.46875 C 0.8125 -2.390625 0.671875 -2.328125 0.671875 -2.140625 C 0.671875 -2.09375 0.671875 -1.796875 1.078125 -1.796875 L 2.3125 -2.515625 C 2.296875 -2.265625 2.28125 -1.609375 2.28125 -1.3125 C 2.28125 -1.21875 2.28125 -0.890625 2.609375 -0.890625 C 2.9375 -0.890625 2.9375 -1.21875 2.9375 -1.3125 C 2.9375 -1.609375 2.9375 -2.265625 2.90625 -2.515625 C 3.515625 -2.1875 3.15625 -2.375 3.84375 -1.96875 C 4.109375 -1.796875 4.140625 -1.796875 4.21875 -1.796875 C 4.421875 -1.796875 4.546875 -1.984375 4.546875 -2.140625 Z M 4.546875 -2.140625 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390635-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.78125 -1.734375 C 4.78125 -2.484375 4.140625 -3.078125 3.40625 -3.28125 C 4.203125 -3.546875 4.640625 -4.03125 4.640625 -4.609375 C 4.640625 -5.421875 3.796875 -6.203125 2.609375 -6.203125 C 1.421875 -6.203125 0.578125 -5.40625 0.578125 -4.609375 C 0.578125 -4.03125 1.03125 -3.53125 1.828125 -3.28125 C 1.078125 -3.078125 0.4375 -2.484375 0.4375 -1.734375 C 0.4375 -0.765625 1.359375 0.109375 2.609375 0.109375 C 3.875 0.109375 4.78125 -0.765625 4.78125 -1.734375 Z M 3.953125 -4.59375 C 3.953125 -4.03125 3.34375 -3.59375 2.609375 -3.59375 C 1.890625 -3.59375 1.265625 -4.03125 1.265625 -4.59375 C 1.265625 -5.109375 1.828125 -5.59375 2.609375 -5.59375 C 3.390625 -5.59375 3.953125 -5.109375 3.953125 -4.59375 Z M 4.09375 -1.75 C 4.09375 -1.0625 3.4375 -0.5 2.609375 -0.5 C 1.78125 -0.5 1.125 -1.0625 1.125 -1.75 C 1.125 -2.359375 1.703125 -2.984375 2.609375 -2.984375 C 3.53125 -2.984375 4.09375 -2.359375 4.09375 -1.75 Z M 4.09375 -1.75 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390635-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.9375 -1.984375 C 4.9375 -2.296875 4.6875 -2.296875 4.53125 -2.296875 L 3.765625 -2.296875 L 3.765625 -5.796875 C 3.765625 -6.125 3.6875 -6.203125 3.34375 -6.203125 L 3.078125 -6.203125 C 2.828125 -6.203125 2.78125 -6.203125 2.65625 -6 L 0.375 -2.4375 C 0.296875 -2.296875 0.296875 -2.28125 0.296875 -2.078125 C 0.296875 -1.75 0.375 -1.6875 0.6875 -1.6875 L 3.203125 -1.6875 L 3.203125 -0.609375 L 2.578125 -0.609375 C 2.421875 -0.609375 2.1875 -0.609375 2.1875 -0.296875 C 2.1875 0 2.4375 0 2.578125 0 L 4.375 0 C 4.53125 0 4.78125 0 4.78125 -0.296875 C 4.78125 -0.609375 4.53125 -0.609375 4.375 -0.609375 L 3.765625 -0.609375 L 3.765625 -1.6875 L 4.53125 -1.6875 C 4.6875 -1.6875 4.9375 -1.6875 4.9375 -1.984375 Z M 3.203125 -2.296875 L 0.984375 -2.296875 L 3.203125 -5.765625 Z M 3.203125 -2.296875 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390635-0-4\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.234375 -0.625 C 3.234375 -0.984375 2.9375 -1.25 2.625 -1.25 C 2.25 -1.25 2 -0.9375 2 -0.625 C 2 -0.265625 2.296875 0 2.609375 0 C 2.984375 0 3.234375 -0.3125 3.234375 -0.625 Z M 3.234375 -0.625 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390635-0-5\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.71875 -3.046875 C 4.71875 -4.890625 3.703125 -6.203125 2.609375 -6.203125 C 1.5 -6.203125 0.5 -4.859375 0.5 -3.046875 C 0.5 -1.203125 1.515625 0.109375 2.609375 0.109375 C 3.734375 0.109375 4.71875 -1.21875 4.71875 -3.046875 Z M 4.03125 -3.15625 C 4.03125 -1.671875 3.390625 -0.5 2.609375 -0.5 C 1.828125 -0.5 1.1875 -1.671875 1.1875 -3.15625 C 1.1875 -4.609375 1.875 -5.59375 2.609375 -5.59375 C 3.34375 -5.59375 4.03125 -4.609375 4.03125 -3.15625 Z M 4.03125 -3.15625 \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -3.058219 -6.11525 L -10.632438 -21.263688 \" transform=\"matrix(1,0,0,-1,20.109,5.916)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194889 1.59436 C -1.097046 0.996917 -0.00170374 0.0990298 0.297022 -0.00053651 C 0.00179544 -0.100119 -1.095245 -0.998064 -1.194804 -1.593766 \" transform=\"matrix(-0.44724,0.89442,0.89442,0.44724,9.47707,27.17989)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 3.058969 -6.11525 L 10.629281 -21.259781 \" transform=\"matrix(1,0,0,-1,20.109,5.916)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.19423 1.592668 C -1.094643 0.996957 -0.00103153 0.100787 0.2977 -0.000530781 C -0.00102794 -0.100113 -1.096354 -0.998069 -1.194173 -1.59553 \" transform=\"matrix(0.44719,0.89442,0.89442,-0.44719,30.73844,27.1749)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390635-0-1\" x=\"17.494\" y=\"8.511\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390635-0-2\" x=\"3.321\" y=\"37.306\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390635-0-3\" x=\"26.437\" y=\"37.311\"/>\n",
       "  <use xlink:href=\"#glyph-1509053111390635-0-4\" x=\"31.667386\" y=\"37.311\"/>\n",
       "  <use xlink:href=\"#glyph-1509053111390635-0-5\" x=\"36.897772\" y=\"37.311\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "TreeView.LabelledTree({3, 2} directed simple Int64 graph, Any[:*, 8, 4.0])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(100)\n",
    "rulenode = rand(RuleNode, grammar, :Real, 10)\n",
    "display(rulenode, grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Evaluate the expression defined by the expression tree:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eval(rulenode, grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get the executable Julia expression from an expression tree:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":(8 * 4.0)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_executable(rulenode, grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sample a random node in the tree:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2{15,4}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(0)\n",
    "sample(rulenode)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sample a random node of type :Real in the expression tree:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2{15,4}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(3)\n",
    "sample(rulenode, :Real, grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sample a random node in the tree and store the location in a NodeLoc object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprRules.NodeLoc(2{15,4}, 1)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(1)\n",
    "loc = sample(NodeLoc, rulenode)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Retrieve the node pointed to by the NodeLoc object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15,"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get(rulenode, loc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Replace the subtree pointed to by loc with a randomly generated subtree:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"59.621pt\" height=\"97.429pt\" viewBox=\"0 0 59.621 97.429\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390636-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390636-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.546875 -2.140625 C 4.546875 -2.296875 4.421875 -2.390625 4.40625 -2.40625 C 4.15625 -2.578125 3.484375 -2.90625 3.21875 -3.046875 L 4.296875 -3.625 C 4.421875 -3.703125 4.546875 -3.765625 4.546875 -3.953125 C 4.546875 -4 4.546875 -4.28125 4.140625 -4.28125 L 2.90625 -3.5625 C 2.9375 -3.828125 2.9375 -4.484375 2.9375 -4.78125 C 2.9375 -4.859375 2.9375 -5.1875 2.609375 -5.1875 C 2.28125 -5.1875 2.28125 -4.859375 2.28125 -4.78125 C 2.28125 -4.484375 2.296875 -3.828125 2.3125 -3.5625 L 1.234375 -4.203125 C 1.09375 -4.28125 1.078125 -4.28125 1 -4.28125 C 0.8125 -4.28125 0.671875 -4.109375 0.671875 -3.953125 C 0.671875 -3.765625 0.78125 -3.703125 0.921875 -3.640625 L 2 -3.046875 L 0.921875 -2.46875 C 0.8125 -2.390625 0.671875 -2.328125 0.671875 -2.140625 C 0.671875 -2.09375 0.671875 -1.796875 1.078125 -1.796875 L 2.3125 -2.515625 C 2.296875 -2.265625 2.28125 -1.609375 2.28125 -1.3125 C 2.28125 -1.21875 2.28125 -0.890625 2.609375 -0.890625 C 2.9375 -0.890625 2.9375 -1.21875 2.9375 -1.3125 C 2.9375 -1.609375 2.9375 -2.265625 2.90625 -2.515625 C 3.515625 -2.1875 3.15625 -2.375 3.84375 -1.96875 C 4.109375 -1.796875 4.140625 -1.796875 4.21875 -1.796875 C 4.421875 -1.796875 4.546875 -1.984375 4.546875 -2.140625 Z M 4.546875 -2.140625 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390636-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.6875 -3.109375 C 4.6875 -5.515625 3.59375 -6.203125 2.640625 -6.203125 C 1.53125 -6.203125 0.53125 -5.375 0.53125 -4.171875 C 0.53125 -3.03125 1.40625 -2.140625 2.5 -2.140625 C 3.015625 -2.140625 3.53125 -2.3125 3.984375 -2.71875 C 3.890625 -1.484375 3.109375 -0.5 2.15625 -0.5 C 2.0625 -0.5 1.703125 -0.515625 1.5 -0.671875 C 1.53125 -0.71875 1.578125 -0.78125 1.578125 -0.953125 C 1.578125 -1.1875 1.40625 -1.390625 1.140625 -1.390625 C 0.890625 -1.390625 0.703125 -1.21875 0.703125 -0.921875 C 0.703125 -0.59375 0.875 0.109375 2.15625 0.109375 C 3.46875 0.109375 4.6875 -1.140625 4.6875 -3.109375 Z M 3.921875 -3.875 C 3.921875 -3.328125 3.375 -2.765625 2.546875 -2.765625 C 1.75 -2.765625 1.21875 -3.421875 1.21875 -4.171875 C 1.21875 -5.03125 1.921875 -5.59375 2.640625 -5.59375 C 3.078125 -5.59375 3.34375 -5.359375 3.515625 -5.15625 C 3.828125 -4.78125 3.921875 -4.578125 3.921875 -3.875 Z M 3.921875 -3.875 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390636-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.78125 -1.734375 C 4.78125 -2.4375 4.3125 -3.03125 3.65625 -3.328125 C 4.21875 -3.6875 4.5 -4.25 4.5 -4.796875 C 4.5 -5.53125 3.765625 -6.203125 2.625 -6.203125 C 1.421875 -6.203125 0.734375 -5.71875 0.734375 -5.03125 C 0.734375 -4.703125 0.984375 -4.5625 1.171875 -4.5625 C 1.390625 -4.5625 1.609375 -4.734375 1.609375 -5.015625 C 1.609375 -5.15625 1.5625 -5.25 1.53125 -5.28125 C 1.828125 -5.59375 2.546875 -5.59375 2.625 -5.59375 C 3.3125 -5.59375 3.8125 -5.234375 3.8125 -4.78125 C 3.8125 -4.484375 3.65625 -4.140625 3.390625 -3.921875 C 3.078125 -3.65625 2.828125 -3.640625 2.46875 -3.625 C 1.890625 -3.578125 1.75 -3.578125 1.75 -3.296875 C 1.75 -2.984375 1.984375 -2.984375 2.140625 -2.984375 L 2.609375 -2.984375 C 3.59375 -2.984375 4.09375 -2.3125 4.09375 -1.734375 C 4.09375 -1.125 3.53125 -0.5 2.625 -0.5 C 2.234375 -0.5 1.46875 -0.609375 1.203125 -1.078125 C 1.25 -1.125 1.328125 -1.1875 1.328125 -1.390625 C 1.328125 -1.625 1.140625 -1.828125 0.890625 -1.828125 C 0.65625 -1.828125 0.4375 -1.671875 0.4375 -1.359375 C 0.4375 -0.46875 1.40625 0.109375 2.625 0.109375 C 3.9375 0.109375 4.78125 -0.8125 4.78125 -1.734375 Z M 4.78125 -1.734375 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390636-0-4\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.6875 -1.921875 C 4.6875 -3.0625 3.8125 -3.953125 2.71875 -3.953125 C 2.203125 -3.953125 1.6875 -3.78125 1.234375 -3.375 C 1.375 -4.78125 2.3125 -5.59375 3.1875 -5.59375 C 3.53125 -5.59375 3.6875 -5.484375 3.734375 -5.4375 C 3.6875 -5.390625 3.640625 -5.328125 3.640625 -5.140625 C 3.640625 -4.90625 3.8125 -4.703125 4.078125 -4.703125 C 4.328125 -4.703125 4.53125 -4.875 4.53125 -5.171875 C 4.53125 -5.65625 4.171875 -6.203125 3.203125 -6.203125 C 1.859375 -6.203125 0.53125 -5 0.53125 -2.984375 C 0.53125 -0.625 1.640625 0.109375 2.625 0.109375 C 3.71875 0.109375 4.6875 -0.734375 4.6875 -1.921875 Z M 4 -1.921875 C 4 -1.0625 3.34375 -0.5 2.625 -0.5 C 1.984375 -0.5 1.59375 -0.984375 1.390625 -1.59375 C 1.296875 -1.828125 1.3125 -2.078125 1.3125 -2.21875 C 1.3125 -2.828125 1.90625 -3.328125 2.671875 -3.328125 C 3.484375 -3.328125 4 -2.65625 4 -1.921875 Z M 4 -1.921875 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390636-0-5\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.9375 -1.984375 C 4.9375 -2.296875 4.6875 -2.296875 4.53125 -2.296875 L 3.765625 -2.296875 L 3.765625 -5.796875 C 3.765625 -6.125 3.6875 -6.203125 3.34375 -6.203125 L 3.078125 -6.203125 C 2.828125 -6.203125 2.78125 -6.203125 2.65625 -6 L 0.375 -2.4375 C 0.296875 -2.296875 0.296875 -2.28125 0.296875 -2.078125 C 0.296875 -1.75 0.375 -1.6875 0.6875 -1.6875 L 3.203125 -1.6875 L 3.203125 -0.609375 L 2.578125 -0.609375 C 2.421875 -0.609375 2.1875 -0.609375 2.1875 -0.296875 C 2.1875 0 2.4375 0 2.578125 0 L 4.375 0 C 4.53125 0 4.78125 0 4.78125 -0.296875 C 4.78125 -0.609375 4.53125 -0.609375 4.375 -0.609375 L 3.765625 -0.609375 L 3.765625 -1.6875 L 4.53125 -1.6875 C 4.6875 -1.6875 4.9375 -1.6875 4.9375 -1.984375 Z M 3.203125 -2.296875 L 0.984375 -2.296875 L 3.203125 -5.765625 Z M 3.203125 -2.296875 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390636-0-6\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.234375 -0.625 C 3.234375 -0.984375 2.9375 -1.25 2.625 -1.25 C 2.25 -1.25 2 -0.9375 2 -0.625 C 2 -0.265625 2.296875 0 2.609375 0 C 2.984375 0 3.234375 -0.3125 3.234375 -0.625 Z M 3.234375 -0.625 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390636-0-7\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.71875 -3.046875 C 4.71875 -4.890625 3.703125 -6.203125 2.609375 -6.203125 C 1.5 -6.203125 0.5 -4.859375 0.5 -3.046875 C 0.5 -1.203125 1.515625 0.109375 2.609375 0.109375 C 3.734375 0.109375 4.71875 -1.21875 4.71875 -3.046875 Z M 4.03125 -3.15625 C 4.03125 -1.671875 3.390625 -0.5 2.609375 -0.5 C 1.828125 -0.5 1.1875 -1.671875 1.1875 -3.15625 C 1.1875 -4.609375 1.875 -5.59375 2.609375 -5.59375 C 3.34375 -5.59375 4.03125 -4.609375 4.03125 -3.15625 Z M 4.03125 -3.15625 \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -3.059344 -6.11525 L -10.910906 -21.822281 \" transform=\"matrix(1,0,0,-1,34.282,5.916)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194453 1.595132 C -1.096619 0.997681 -0.00128328 0.099767 0.297443 0.000194404 C -0.00128149 -0.10113 -1.094854 -0.997318 -1.194424 -1.593023 \" transform=\"matrix(-0.44722,0.89442,0.89442,0.44722,23.37113,27.73778)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 3.057844 -6.11525 L 10.628156 -21.259781 \" transform=\"matrix(1,0,0,-1,34.282,5.916)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194733 1.591662 C -1.095146 0.995951 -0.00153464 0.0997812 0.298944 0.00195691 C 0.000215854 -0.097625 -1.095111 -0.995581 -1.194676 -1.596536 \" transform=\"matrix(0.44719,0.89442,0.89442,-0.44719,44.91144,27.1749)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -17.231219 -34.462906 L -24.805438 -49.611344 \" transform=\"matrix(1,0,0,-1,34.282,5.916)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194285 1.59511 C -1.096441 0.997666 -0.0010989 0.0997795 0.297627 0.000213144 C -0.00109351 -0.101116 -1.094641 -0.997315 -1.194199 -1.593017 \" transform=\"matrix(-0.44724,0.89442,0.89442,0.44724,9.47667,55.52667)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -11.114031 -34.462906 L -3.262469 -50.169938 \" transform=\"matrix(1,0,0,-1,34.282,5.916)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.197172 1.594392 C -1.094109 0.996939 -0.000535757 0.100752 0.298189 -0.000572866 C -0.000537554 -0.100145 -1.095873 -0.99806 -1.197201 -1.593764 \" transform=\"matrix(0.44722,0.89442,0.89442,-0.44722,31.0195,56.0846)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -3.059344 -62.810563 L -10.633563 -77.959 \" transform=\"matrix(1,0,0,-1,34.282,5.916)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196348 1.592373 C -1.095011 0.996676 0.000331081 0.0987895 0.299057 -0.000776831 C -0.00141054 -0.0986123 -1.094958 -0.994811 -1.196263 -1.595754 \" transform=\"matrix(-0.44724,0.89442,0.89442,0.44724,23.65007,83.87349)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 3.057844 -62.810563 L 10.632062 -77.959 \" transform=\"matrix(1,0,0,-1,34.282,5.916)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196934 1.594412 C -1.093881 0.996963 -0.000334382 0.100765 0.298386 -0.000564784 C -0.000339772 -0.100131 -1.095682 -0.998018 -1.197019 -1.593715 \" transform=\"matrix(0.44724,0.89442,0.89442,-0.44724,44.91393,83.87349)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390636-0-1\" x=\"31.667\" y=\"8.511\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390636-0-1\" x=\"17.494\" y=\"36.858\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390636-0-2\" x=\"3.321\" y=\"65.652\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390636-0-1\" x=\"31.667\" y=\"65.204\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390636-0-3\" x=\"17.494\" y=\"93.999\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390636-0-4\" x=\"45.84\" y=\"93.999\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390636-0-5\" x=\"40.61\" y=\"37.311\"/>\n",
       "  <use xlink:href=\"#glyph-1509053111390636-0-6\" x=\"45.840386\" y=\"37.311\"/>\n",
       "  <use xlink:href=\"#glyph-1509053111390636-0-7\" x=\"51.070772\" y=\"37.311\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "TreeView.LabelledTree({7, 6} directed simple Int64 graph, Any[:*, :*, 9, :*, 3, 6, 4.0])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(28)\n",
    "insert!(rulenode, loc, rand(RuleNode, grammar, :Real, 3))\n",
    "display(rulenode, grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Minimum Depth Map\n",
    "\n",
    "Compute the minimum depth of all possible subtrees for each production rule:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16-element Array{Int64,1}:\n",
       " 0\n",
       " 1\n",
       " 1\n",
       " 0\n",
       " 0\n",
       " 0\n",
       " 1\n",
       " 0\n",
       " 0\n",
       " 0\n",
       " 0\n",
       " 0\n",
       " 0\n",
       " 0\n",
       " 0\n",
       " 0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dmap = mindepth_map(grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compute the minimum depth of all possible subtrees starting from a given start symbol:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mindepth(grammar, :Real, dmap) #zero for terminals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Expression Iterator\n",
    "\n",
    "Iterate over all possible expressions of a grammar up to depth 2:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6-element Array{ExprRules.RuleNode,1}:\n",
       " 1{2,2}\n",
       " 1{2,3}\n",
       " 1{3,2}\n",
       " 1{3,3}\n",
       " 2,    \n",
       " 3,    "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grammar = @grammar begin\n",
    "    Real = Real + Real\n",
    "    Real = 1 | 2\n",
    "end\n",
    "iter = ExpressionIterator(grammar, 2, :Real)\n",
    "collect(iter)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Count the number of expressions of a grammar up to depth 2:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count_expressions(grammar, 2, :Real)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
